package com.tanhua.bg.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.bg.mapper.FreezingMapper;
import com.tanhua.bg.mapper.UnfreezingMapper;
import com.tanhua.bg.mapper.UserInfoMapper;
import com.tanhua.bg.mapper.UserMapper;
import com.tanhua.bg.pojo.Freezing;
import com.tanhua.bg.pojo.Unfreezing;
import com.tanhua.bg.pojo.User;
import com.tanhua.bg.pojo.UserInfo;
import com.tanhua.bg.vo.PageResult;
import com.tanhua.bg.vo.UsersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zongjie
 * @Description: 用户管理模块service
 * @date 2021/10/13  9:53
 */

@Service
public class UserManagerService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private FreezingMapper freezingMapper;

    @Autowired
    private UnfreezingMapper unfreezingMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    public PageResult queryUsers(Integer page, Integer pageSize, Integer userId, String nickname, String city) {
        // 先判断有没有传入查询条件
        // 构建分页结果对象
        PageResult pageResult = new PageResult();

        // 没有条件 直接分页查询
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);
        // 调用mapper查询
        List<User> userList = userMapper.selectList(null);
        pageResult.setCounts(userList.size());
        pageResult.setPages(pageResult.getCounts() / pageSize + 1);
        // 返回的usersVo的集合
        List<UsersVo> usersVoList = new ArrayList<>();
        // 根据userId查询的条件
        if (userId != null) {
            // 根据用户id查询用户信息
            UserInfo userInfo = userInfoService.queryUserInfoById(Long.parseLong(userId.toString()));
            UsersVo usersVo = new UsersVo();
            // 封装usersvo对象
            usersVo.setId(userInfo.getUserId());
            usersVo.setCity(userInfo.getCity());
            usersVo.setAge(userInfo.getAge());
            usersVo.setSex(userInfo.getSex() == 1 ? "男" : (userInfo.getSex() == 2 ? "女" : "未知"));
            usersVo.setPersonalSignature(userInfo.getPersonalSignature());
            usersVo.setCountBeLiked(userInfo.getCountBeLiked());
            usersVo.setCountMatching(userInfo.getCountMatching());
            usersVo.setCountLiked(userInfo.getCountLiked());
            usersVo.setCreated(userInfo.getCreated().getTime());
            usersVo.setIncome(Integer.parseInt(userInfo.getIncome()));
            usersVo.setLastActiveTime(userInfo.getLastActiveTime());
            usersVo.setLastLoginLocation(userInfo.getLastLoginLocation());
            usersVo.setUserStatus(userInfo.getUserStatus());
            usersVo.setNickname(userInfo.getNickName());
            usersVoList.add(usersVo);
            // 补充pageResult信息
            pageResult.setCounts(usersVoList.size());
            pageResult.setPages(pageResult.getCounts() / pageSize + 1);
            pageResult.setItems(usersVoList);
            return pageResult;
        }
        // 根据nickname昵称查询的条件
        if (StrUtil.isNotEmpty(nickname)){
            // 根据昵称查询所有满足条件的用户信息  并分页
            IPage<UserInfo> userInfoPage = new Page<>(page,pageSize);
            QueryWrapper<UserInfo> query = new QueryWrapper<>();
            query.eq("nick_name",nickname);
            userInfoPage=userInfoMapper.selectPage(userInfoPage,query);
            List<UserInfo> userInfoList = userInfoPage.getRecords();

            List<UsersVo> usersVoList1 = new ArrayList<>();
            for (UserInfo userInfo : userInfoList) {
                UsersVo usersVo = new UsersVo();
                usersVo.setId(userInfo.getUserId());
                usersVo.setCity(userInfo.getCity());
                usersVo.setAge(userInfo.getAge());
                usersVo.setSex(userInfo.getSex() == 1 ? "男" : (userInfo.getSex() == 2 ? "女" : "未知"));
                usersVo.setPersonalSignature(userInfo.getPersonalSignature());
                usersVo.setCountBeLiked(userInfo.getCountBeLiked());
                usersVo.setCountMatching(userInfo.getCountMatching());
                usersVo.setCountLiked(userInfo.getCountLiked());
                usersVo.setCreated(userInfo.getCreated().getTime());
                usersVo.setIncome(Integer.parseInt(userInfo.getIncome()));
                usersVo.setLastActiveTime(userInfo.getLastActiveTime());
                usersVo.setLastLoginLocation(userInfo.getLastLoginLocation());
                usersVo.setUserStatus(userInfo.getUserStatus());
                usersVo.setNickname(userInfo.getNickName());
                usersVoList1.add(usersVo);
            }
            usersVoList=usersVoList1;
            // 进行分页查询操作

            pageResult.setCounts(usersVoList.size());
            pageResult.setPages(pageResult.getCounts() / pageSize + 1);
            pageResult.setItems(usersVoList);
            return pageResult;
        }
        // 根据城市查询
        if (StrUtil.isNotEmpty(city)){
            List<UserInfo> userInfoList1 = userInfoService.queryUserInfoByCity(city);
            // 根据昵称查询所有满足条件的用户信息  并分页
            IPage<UserInfo> userInfoPage = new Page<>(page,pageSize);
            QueryWrapper<UserInfo> query = new QueryWrapper<>();
            query.eq("city",city);
            userInfoPage=userInfoMapper.selectPage(userInfoPage,query);
            List<UserInfo> userInfoList = userInfoPage.getRecords();
            //填充usersVo信息
            List<UsersVo> usersVoList2 = new ArrayList<>();
            for (UserInfo userInfo : userInfoList) {
                UsersVo usersVo = new UsersVo();
                usersVo.setId(userInfo.getUserId());
                usersVo.setCity(userInfo.getCity());
                usersVo.setAge(userInfo.getAge());
                usersVo.setSex(userInfo.getSex() == 1 ? "男" : (userInfo.getSex() == 2 ? "女" : "未知"));
                usersVo.setPersonalSignature(userInfo.getPersonalSignature());
                usersVo.setCountBeLiked(userInfo.getCountBeLiked());
                usersVo.setCountMatching(userInfo.getCountMatching());
                usersVo.setCountLiked(userInfo.getCountLiked());
                usersVo.setCreated(userInfo.getCreated().getTime());
                usersVo.setIncome(Integer.parseInt(userInfo.getIncome()));
                usersVo.setLastActiveTime(userInfo.getLastActiveTime());
                usersVo.setLastLoginLocation(userInfo.getLastLoginLocation());
                usersVo.setUserStatus(userInfo.getUserStatus());
                usersVo.setNickname(userInfo.getNickName());
                usersVoList2.add(usersVo);
            }
            usersVoList=usersVoList2;
            pageResult.setCounts(userInfoList1.size());
            pageResult.setPages(userInfoList1.size() / pageSize + 1);
            pageResult.setItems(usersVoList);
            return pageResult;
        }
        // 到这  说明没有查询条件  分页获取所有用户的信息集合
        // 根据昵称查询所有满足条件的用户信息  并分页
        IPage<UserInfo> userInfoPage = new Page<>(page,pageSize);
        userInfoPage=userInfoMapper.selectPage(userInfoPage,null);
        List<UserInfo> userInfoList = userInfoPage.getRecords();
        for (UserInfo userInfo : userInfoList) {
            UsersVo usersVo = new UsersVo();
            usersVo.setId(userInfo.getUserId());
            usersVo.setCity(userInfo.getCity());
            usersVo.setAge(userInfo.getAge());
            usersVo.setSex(userInfo.getSex() == 1 ? "男" : (userInfo.getSex() == 2 ? "女" : "未知"));
            usersVo.setPersonalSignature(userInfo.getPersonalSignature());
            usersVo.setCountBeLiked(userInfo.getCountBeLiked());
            usersVo.setCountMatching(userInfo.getCountMatching());
            usersVo.setCountLiked(userInfo.getCountLiked());
            usersVo.setCreated(userInfo.getCreated().getTime());
            usersVo.setIncome(Integer.parseInt(userInfo.getIncome()));
            usersVo.setLastActiveTime(userInfo.getLastActiveTime());
            usersVo.setLastLoginLocation(userInfo.getLastLoginLocation());
            usersVo.setUserStatus(userInfo.getUserStatus());
            usersVo.setNickname(userInfo.getNickName());
            usersVoList.add(usersVo);
        }
        // 填充pageResult信息
        pageResult.setItems(usersVoList);
        return pageResult;
    }

    /**
     * 根据用户id查询用户的所有信息
     *
     * @param userId
     * @return
     */
    public UsersVo queryUserInfoById(Long userId) {
        // 查询用户信息
        UserInfo userInfo = userInfoService.queryUserInfoById(userId);
        // 封装usersVo对象
        UsersVo usersVo = new UsersVo();
        usersVo.setId(userInfo.getUserId());
        usersVo.setCity(userInfo.getCity());
        usersVo.setAge(userInfo.getAge());
        usersVo.setSex(userInfo.getSex() == 1 ? "男" : (userInfo.getSex() == 2 ? "女" : "未知"));
        usersVo.setPersonalSignature(userInfo.getPersonalSignature());
        usersVo.setCountBeLiked(userInfo.getCountBeLiked());
        usersVo.setCountMatching(userInfo.getCountMatching());
        usersVo.setCountLiked(userInfo.getCountLiked());
        usersVo.setCreated(userInfo.getCreated().getTime());
        usersVo.setIncome(Integer.parseInt(userInfo.getIncome()));
        usersVo.setLastActiveTime(userInfo.getLastActiveTime());
        usersVo.setLastLoginLocation(userInfo.getLastLoginLocation());
        // 获取用户表中的手机号
        Long userId1 = userInfo.getUserId();
        // 根据用户表查询手机号
        usersVo.setMobile(userMapper.selectById(userId).getMobile());
        usersVo.setLogo(userInfo.getLogo());
        usersVo.setNickname(userInfo.getNickName());
        usersVo.setUserStatus(userInfo.getUserStatus());
        usersVo.setCity(userInfo.getCity());
        usersVo.setOccupation(userInfo.getOccupation());
        usersVo.setTags(userInfo.getTags());
        return usersVo;
    }

    /**
     * 冻结用户
     *
     * @param param
     * @return
     */
    public Boolean freezingUser(Map<String, Object> param) {
        // 获取param中的参数
        Integer userId = Integer.parseInt(param.get("userId").toString());
        Integer freezingTime = Integer.parseInt(param.get("freezingTime").toString());
        Integer freezingRange = Integer.parseInt(param.get("freezingRange").toString());
        String reasonsForFreezing = param.get("reasonsForFreezing").toString();
        String frozenRemarks = param.get("frozenRemarks").toString();
        // 判断用户信息表中的用户状态 有没有被冻结
        UserInfo userInfo = userInfoService.queryUserInfoById(Long.parseLong(userId.toString()));
        Integer userStatus = Integer.parseInt(userInfo.getUserStatus());
        if (userStatus == 2) {
            //  这里说明已经被冻结 直接返回
            return false;
        }
        // 到这里说明没被冻结  冻结用户
        // 1.将用户信息表中的用户状态改为冻结
        userInfoService.updateUserStatus(Long.parseLong(userId.toString()));
        // 2.将该用户写入用户冻结表中
        Freezing freezing = new Freezing();
        freezing.setUserId(userId);
        freezing.setFreezingRange(freezingRange);
        freezing.setFreezingTime(freezingTime);
        freezing.setFrozenRemarks(frozenRemarks);
        freezing.setReasonsForFreezing(reasonsForFreezing);
        freezingMapper.insert(freezing);
        return true;
    }

    /**
     * 用户解冻操作
     *
     * @param param
     * @return
     */
    public Boolean unfreezingUser(Map<String, Object> param) {
        // 获取param中的参数
        // 要解冻的用户id
        Integer userId = Integer.parseInt(param.get("userId").toString());
        // 解冻原因
        String reasonsForThawing = param.get("reasonsForThawing").toString();
        // 判断用户状态
        UserInfo userInfo = userInfoService.queryUserInfoById(Long.parseLong(userId.toString()));
        Integer userStatus = Integer.parseInt(userInfo.getUserStatus());
        if (userStatus == 1) {
            // 用户状态正常  直接返回false
            return false;
        }
        // 用户解冻
        // 1. 修改用户信息表中的用户状态
        userInfoService.updateUserStatus(Long.parseLong(userId.toString()));
        // 2. 删除该用户对应的用户冻结表中的信息
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", userId);
        freezingMapper.deleteByMap(map);
        // 3. 向用户解冻表中添加信息
        Unfreezing unfreezing = new Unfreezing();
        unfreezing.setUserId(userId);
        unfreezing.setReasonsForThawing(reasonsForThawing);
        unfreezingMapper.insert(unfreezing);
        return true;
    }


    /**
     *  分页查询方法
     * @param page
     * @param pageSize
     * @return
     */
    public List<UserInfo> queryByPage(Integer page,Integer pageSize){
        IPage<UserInfo> userInfoPage = new Page<>(page,pageSize);
        userInfoPage=userInfoMapper.selectPage(userInfoPage,null);
        List<UserInfo> userInfoList = userInfoPage.getRecords();
        return userInfoList;
    }
}
